package com.julu.o2o.bean;

import java.io.Serializable;
import java.util.List;

import com.jfinal.plugin.activerecord.Page;

public class Paging<T> implements Serializable {
	private static final long serialVersionUID = -238537086067117144L;
	// 分页查询中每页最多显示记录数
	public static int PAGE_SIZE = 12;
	// 分页条大小
	public static int PAGINATION_SIZE = 5;

	private List<T> list; // list result of this page
	private int pageNumber; // page number
	private int pageSize; // result amount of this page
	private int totalPage; // total page
	private int totalRow;

	/**
	 * Constructor.
	 * 
	 * @param list
	 *            the list of paginate result
	 * @param pageNumber
	 *            the page number
	 * @param pageSize
	 *            the page size
	 * @param totalPage
	 *            the total page of paginate
	 * @param totalRow
	 *            the total row of paginate
	 */
	public Paging(List<T> list, int pageNumber, int pageSize, int totalPage, int totalRow) {
		this.list = list;
		this.pageNumber = pageNumber;
		this.pageSize = pageSize;
		this.totalPage = totalPage;
		this.totalRow = totalRow;
	}

	/**
	 * Return list of this page.
	 */
	public List<T> getList() {
		return list;
	}

	/**
	 * Return page number.
	 */
	public int getPageNumber() {
		return pageNumber;
	}

	/**
	 * Return page size.
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * Return total page.
	 */
	public int getTotalPage() {
		return totalPage;
	}

	/**
	 * Return total row.
	 */
	public int getTotalRow() {
		return totalRow;
	}

	public boolean isFirstPage() {
		return pageNumber == 1;
	}

	public boolean isLastPage() {
		return pageNumber == totalPage;
	}

	/**
	 * 设置分页显示最大记录数
	 * 
	 * @param size
	 */
	public void setPageSize(int size) {
		PAGE_SIZE = size;
	}

	/**
	 * 设置分页条大小
	 * 
	 * @param size
	 */
	public void setPaginationSize(int size) {
		PAGINATION_SIZE = size;
	}
	public static <T> Paging<T> from(Page<T> page) {	
		return new Paging<T>(page.getList(), 
				page.getPageNumber(), page.getPageSize(), 
				page.getTotalPage(),
				page.getTotalRow());
	}
}
